<script setup>
import {
  Edit,
  Delete,
  Close
} from '@element-plus/icons-vue'


import {ref} from 'vue'


const contentTypes = ref([])


const versionSeasons = ref([])
const resourceTypes = ref([
  {
    "id": 0,
    "resourceValue": "文字",
  },
  {
    "id": 1,
    "resourceValue": "表情图片",
  }
  ,
  {
    "id": 2,
    "resourceValue": "表情包图片",
  }


])

const memberTypes = ref([
  {
    "id": 0,
    "resourceValue": "普通用户资源",
  },
  {
    "id": 1,
    "resourceValue": "会员资源",
  }
])


//用户搜索时选中的分类id
const resourceType = ref('')

const account = ref('')

//用户搜索时选中的发布状态
const state = ref('')


const rewardSeasonId = ref('')

const mileStones = ref([])


//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(15)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  publishResourceList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  publishResourceList()
}


import {
  resoucePublishDeleteService,
  resourcePublishAddService, resourcePublishEditService,
  resourcePublishListService
} from '@/api/resourcePublish.js'


import {Plus} from '@element-plus/icons-vue'
import {QuillEditor} from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const dataModel = ref({})


//导入token
import {useTokenStore} from '@/stores/token.js';

const tokenStore = useTokenStore();


//上传成功的回调函数
const uploadHeadPicSuccess = (result) => {
  dataModel.value.fileUrl = result.data.fileUrl;
  dataModel.value.fileName = result.data.fileName;
}



//添加文章
import {ElMessage} from 'element-plus'

const publishResourceAdd = async (clickState) => {
  //把发布状态赋值给数据模型
  dataModel.value.state = clickState;
  dataModel.value.personalSignaturePic =fileList;
  //调用接口
  let result = await resourcePublishAddService(dataModel.value);

  ElMessage.success(result.msg ? result.msg : '添加成功');

  //让抽屉消失
  visibleDrawer.value = false;

  //刷新当前列表
  publishResourceList()
}

//编辑分类
const publishInformationEdit = async () => {

  //调用接口
  let result = await resourcePublishEditService(dataModel.value);

  ElMessage.success(result.msg ? result.msg : '修改成功')

  //调用获取所有分类的函数
  publishResourceList();

  //隐藏弹窗
  visibleDrawer.value = false;
}


//获取文章列表数据
const publishResourceList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    type:resourceType.value !==''?resourceType.value:null
  }
  let result = await resourcePublishListService(params);

  total.value = result.data.total;
  mileStones.value = result.data.records;

}

const deletePublishResource = (row) => {
  //提示用户  确认框

  ElMessageBox.confirm(
      '你确认要删除该资源信息吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //调用接口
        let result = await resoucePublishDeleteService(row.id);
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        //刷新列表
        publishResourceList();
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了删除',
        })
      })
}

publishResourceList();



//删除分类
import {ElMessageBox} from 'element-plus'
import {informationDeleteService} from "@/api/resourceInformation.js";


//定义变量,控制标题的展示
const title = ref('')
const showMileStone = (row) => {
  visibleDrawer.value = true, title.value = '编辑资源'
  dataModel.value.id= row.id;
  dataModel.value.type=row.type;
  dataModel.value.fileName=row.fileName;
  dataModel.value.fileUrl=row.fileUrl;
  dataModel.value.memberType=row.memberType;
  dataModel.value.content=row.content;
}

//清空模型的数据
const clearData = () => {

  dataModel.value.id= '';
  dataModel.value.type='';
  dataModel.value.fileName='';
  dataModel.value.fileUrl='';
  dataModel.value.memberType='';
  dataModel.value.content='';
}


const delHeadPicImg = () => {
  dataModel.value.fileUrl = '';
  dataModel.value.fileName = '';
}




const fileList = ref([])

</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>用户发布资源管理</span>
        <div class="extra">
          <el-button type="primary" @click="visibleDrawer = true; title = '添加资源'; clearData()">添加资源
          </el-button>
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="账号类型：">
        <el-select placeholder="请选择" v-model="resourceType">
          <el-option v-for="c in resourceTypes" :key="c.id" :label="c.resourceValue" :value="c.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="publishResourceList">搜索</el-button>
        <el-button @click="resourceType = ''">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="mileStones" border style="width: 100%">
      <el-table-column label="资源类型" >
        <template v-slot="scope">
          <span v-if="scope.row.type==0">文字</span>
          <span v-if="scope.row.type==1">表情图片</span>
          <span v-if="scope.row.type==2">表情包图片</span>
          <span v-else ></span>
        </template>
      </el-table-column>
      <el-table-column label="文本内容" prop="content"></el-table-column>
      <el-table-column label="图片资源">
        <template v-slot="scope">
          <img v-if="scope.row.fileUrl" :src="scope.row.fileUrl" width="40" height="40" class="head_pic" />
        </template>
      </el-table-column>
      <el-table-column label="会员类型">
        <template v-slot="scope">
          <span v-if="scope.row.memberType==0">普通用户资源</span>
          <span v-if="scope.row.memberType!=0">会员资源</span>
          <span v-else ></span>
        </template>
      </el-table-column>



      <el-table-column label="创建时间" prop="createTime"></el-table-column>

      <el-table-column label="操作" >
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showMileStone(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deletePublishResource(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>


    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total"
                   @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end"/>


    <!-- 抽屉 -->
    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
      <el-form :model="dataModel" label-width="100px">
        <el-form-item label="资源类型：">
          <el-select placeholder="请选择" v-model="dataModel.type">
            <el-option v-for="c in resourceTypes" :key="c.id" :label="c.resourceValue" :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员类型：">
          <el-select placeholder="请选择" v-model="dataModel.memberType">
            <el-option v-for="c in memberTypes" :key="c.id" :label="c.resourceValue" :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="文本内容" v-if="dataModel.type === 0 ">
          <el-input v-model="dataModel.content" type="textarea" style="width: 500px"/>
        </el-form-item>



        <el-form-item label="图片资源" v-if="dataModel.type === 1 || dataModel.type === 2 ">
          <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false"
                     action="/api/admin/upload/uploadFile/v2" name="file"
                     :headers="{ 'Authorization': tokenStore.token }"
                     :on-success="uploadHeadPicSuccess">
            <img v-if="dataModel.fileUrl" :src="dataModel.fileUrl" class="avatar"/>
            <el-icon v-else class="avatar-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
          <span v-if="show">{{ dataModel.fileUrl }}</span>
          <el-icon v-if="dataModel.fileUrl" @click="delHeadPicImg()">
            <Close/>
          </el-icon>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" @click="title == '添加资源' ? publishResourceAdd() : publishInformationEdit()">保存</el-button>

        </el-form-item>
      </el-form>
    </el-drawer>


  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}


/* 抽屉样式 */
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>